<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="条码编号" prop="barcodeNumber">
        <el-input
          v-model="queryParams.barcodeNumber"
          placeholder="请输入条码编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="资产名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入资产名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="资产编号" prop="assetNumber">
        <el-input
          v-model="queryParams.assetNumber"
          placeholder="请输入资产编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="资产分类" prop="assetType">
        <el-select
          v-model="queryParams.assetType"
          placeholder="请选择资产分类"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.ASSET_CLASSIFICATION)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="specificationModel">
        <el-input
          v-model="queryParams.specificationModel"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="数量" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="计量单位" prop="unitOfMeasurement">
        <el-input
          v-model="queryParams.unitOfMeasurement"
          placeholder="请输入计量单位"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="存放位置" prop="storageLocation">
        <el-input
          v-model="queryParams.storageLocation"
          placeholder="请输入存放位置"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="业务状态" prop="businessStatus">
        <el-select
          v-model="queryParams.businessStatus"
          placeholder="请选择业务状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.BUSINESS_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="成新率" prop="newnessRate">
        <el-select
          v-model="queryParams.newnessRate"
          placeholder="请选择成新率"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.NEWNESS_RATE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['asset:manager:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="条码编号" align="center" width="160" prop="barcodeNumber" fixed="left"/>
      <el-table-column label="资产编号" align="center" width="160" prop="assetNumber" />
      <el-table-column label="资产名称" align="center" width="160" prop="title" />
      <el-table-column label="资产分类" align="center" width="160" prop="assetType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ASSET_CLASSIFICATION" :value="scope.row.assetType" />
        </template>
      </el-table-column>
      <el-table-column label="品牌" align="center" width="160" prop="brand" />
      <el-table-column label="规格型号" align="center" width="100" prop="specificationModel" />
      <el-table-column label="数量" align="center" width="100" prop="number" />
      <el-table-column label="计量单位" align="center" width="160" prop="unitOfMeasurement">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.UNIT_OF_MEASUREMENT" :value="scope.row.unitOfMeasurement" />
        </template>
      </el-table-column>
      <el-table-column label="资产原值" align="center" width="160" prop="originalValue" />
      <el-table-column
        label="生产时间"
        align="center"
        prop="obtainTime"
        :formatter="dateFormatter2"
        width="180px"
      />
      <el-table-column label="管理部门" align="center" prop="managementDeptName" width="160" />
      <el-table-column label="管理人" align="center" prop="managementUserName" width="160" />
      <el-table-column label="使用部门" align="center" prop="useDeptName" width="160" />
      <el-table-column label="使用人" align="center" prop="useUserName" width="160" />
      <el-table-column label="存放地点" align="center" prop="storageLocation" width="160" />
      <el-table-column label="业务状态" align="center" prop="businessStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.BUSINESS_STATUS" :value="scope.row.businessStatus" />
        </template>
      </el-table-column>
      <el-table-column label="成新率" align="center" prop="newnessRate" width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.NEWNESS_RATE" :value="scope.row.newnessRate" />
        </template>
      </el-table-column>
      <el-table-column
        label="财务记账日期"
        align="center"
        prop="financialAccountingTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="160px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="copy(scope.row)"
            v-hasPermi="['asset:manager:update']"
          >
            复制
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['asset:manager:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['asset:manager:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ManagerForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter, dateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { ManagerApi, ManagerVO } from '@/api/asset/manager'
import ManagerForm from './ManagerForm.vue'

/** 资产管理 列表 */
defineOptions({ name: 'Manager' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ManagerVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  assetNumber: undefined,
  barcodeNumber: undefined,
  storageLocation: undefined,
  title: undefined,
  assetType: undefined,
  brand: undefined,
  specificationModel: undefined,
  number: undefined,
  unitOfMeasurement: undefined,
  businessStatus: undefined,
  newnessRate: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ManagerApi.getManagerPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

const copy = (row:any)=>{
  const {id,files,barcodeNumber,createTime,...oteher} = row
  localStorage.setItem("copy_edit",JSON.stringify(oteher))
  message.success("复制成功")
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ManagerApi.deleteManager(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ManagerApi.exportManager(queryParams)
    download.excel(data, '资产管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
